//
//  CommunityViewCtrl.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/15.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "CommunityViewCtrl.h"
#import "AddNoteViewCtrl.h"
#import "ComNoteListTableview.h"
#import "NetRequest+community.h"
#import "NoteMode.h"
#import "ImageMode.h"
#import "NoteDetailviewCtrl.h"
#import "NoteCommentMode.h"


@interface CommunityViewCtrl () <MYRefreshTableViewDelegate>


@property (assign, nonatomic) BOOL isPullLoad;

@property (assign, nonatomic) NSInteger pageNo;

@property(nonatomic,strong)NSMutableArray *notelistArrs;

@property (weak, nonatomic) IBOutlet ComNoteListTableview *notelistTableview;


@end

@implementation CommunityViewCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Community";
    [self layoutNotelistTableview];
    [self navBarRightButtonWithImageName:@"photo"];
    //先屏蔽
//    [self requestNoteList];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 界面
- (void)layoutNotelistTableview {
    WeakSelf(ws);
    self.notelistTableview.refreshDelegate = self;
    self.notelistTableview.notelists = self.notelistArrs;
    self.notelistTableview.didSelectedRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
        NoteMode *mode = [self.notelistArrs objectAtIndex:indexPath.row];
        NoteDetailviewCtrl *detailCtrl = [[NoteDetailviewCtrl alloc] initWithNibName:@"NoteDetailviewCtrl" bundle:nil];
        NoteDetailMode *deMode = [[NoteDetailMode alloc] initWithNoteMode:mode];
        deMode.comments = [NoteCommentMode testComModes];
        detailCtrl.noteMode = deMode;
        [ws.navigationController pushViewController:detailCtrl animated:YES];
    };
    self.notelistTableview.revLikeBlock = ^(NSInteger index) {
        NSLog(@"revLike %ld",(long)index);
        
    };
}

#pragma mark -网络请求
- (void)requestNoteList {
    _isPullLoad  = NO;
    [self requestNotelist:NO];
}

- (void)requestNotelist:(BOOL)_isMore {
    NSDictionary *params = [self loadRequestParams:_isMore];
    WeakSelf(ws);
    if (!_isPullLoad) {//不是下拉和上拉
        [self.loadingView startAnimating];
    }
    NSURLSessionDataTask *dataTask =
    [[NetRequest sharedInstance] getNotelistDictionary:params success:^(NSMutableArray *notelists,NSInteger totalCount,NSInteger totalPages) {
        if (!ws.isPullLoad) {
            [ws.loadingView stopAnimating];
            [ws.loadingView removeFromSuperview];
        } else {
            [SVProgressHUD dismiss];
        }
        ws.notelistTableview.hidden = NO;
        
        if(notelists.count > 0)
        {
            
            if(ws.pageNo == 1)
            {
                [ws.notelistArrs removeAllObjects];
            }
            
            [ws.notelistTableview resetNoMoreData];
            
            [ws.notelistArrs addObjectsFromArray:notelists];
            
            ws.notelistTableview.notelists = ws.notelistArrs;
        }
        else
        {
            if(ws.pageNo == 1  && !_isMore)
            {
                [ws.notelistTableview.notelists removeAllObjects];
            }
        }
        
        if (ws.pageNo < totalPages) {
            // 当有数据的时候，页码加1处理
            ws.pageNo++;
        }
        else
        {
            ws.notelistTableview.mj_footer.hidden = NO;
            [ws.notelistTableview noMoreData];
        }
        
        [ws endRefreshLoading:YES];
    } fail:^(RequestError *error) {
        
    }];
    
    [self addSessionDataTask:dataTask];
}

- (NSDictionary *)loadRequestParams:(BOOL)_isMore {
    NSInteger page;
    
    if (_isMore) {
        page = _pageNo;
    }
    else
    {
        _pageNo = 1;
        page = 1;
    }
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:@(page) forKey:@"pageNo"];
    [dict setObject:@(10) forKey:@"pageSize"];
    
    return dict;
}
#pragma mark - action事件
- (void)rightButtonclickEvent:(id)sender {
    AddNoteViewCtrl *addCtrl = [[AddNoteViewCtrl alloc] initWithNibName:@"AddNoteViewCtrl" bundle:nil];
    [self.navigationController pushViewController:addCtrl animated:YES];
}

#pragma mark -MYRefreshCollectionViewDelegate
- (void)loadedData:(BaseCustomTablView *)tableView withLoadType:(MYLoadDataTypeOne)type {
    switch (type) {
        case MYLoadDataTypeRefreshOne:
            [self requestNotelist:NO];
            break;
        case MYLoadDataTypeLoadMoreOne:
            [self requestNotelist:YES];
        default:
            break;
    }
}

- (void)endRefreshLoading:(BOOL)_success {
    if (_success) {
        if (self.notelistArrs.count > 0) {
            [self.notelistTableview reloadData];
//            self.noDataSourceView.hidden = YES;
        } else {
//            self.noDataSourceView.hidden = NO;
//            self.noDataSourceView.failedIcon = @"noOrder.png";
//            self.noDataSourceView.failedText = @"暂无订单，赶快去下单吧";
        }
    }
    [self.notelistTableview endRefreshing];
}

#pragma mark - getter
- (NSMutableArray *)notelistArrs {
    if (_notelistArrs == nil) {
        _notelistArrs  = [NSMutableArray array];
        //测试代码
        for (NSInteger i = 0; i < 10;i++) {
            NoteMode *mode = [[NoteMode alloc] init];
            
            UserMode  *uMode  = [[UserMode alloc] init];
            uMode.headPortraitUrl = @"http://img2.100bt.com/upload/ttq/20131103/1383470553132_middle.jpg";
            uMode.userName =@"宋旭华";
            mode.userMode = uMode;
            mode.price = [NSString stringWithFormat:@"%ld",(long)i];
            mode.noteDateStr = [NSString stringWithFormat:@"%ld min ago",(long)i];
            mode.noteTitle = [NSString stringWithFormat:@"%ld 我是宋旭华测试标题1我是宋旭华测试标题2我是宋旭华测试标题3我是宋旭华测试标题4我是宋旭华测试标题6我是宋旭华测试标题7",(long)i] ;
            mode.noteContent = [NSString stringWithFormat:@"%ld 我是宋旭华测试内容1我是宋旭华测试内容2我是宋旭华测试内容3我是宋旭华测试内容4我是宋旭华测试内容5我是宋旭华测试内容6我是宋旭华测试内容7",(long)i] ;
            
            mode.noteImages = [ImageMode testNetImgs];
            mode.notecateory = [NSString stringWithFormat:@"cateory %ld",i];
            mode.dicussionCount = [NSString stringWithFormat:@"%ld",i];
            mode.likeCount = [NSString stringWithFormat:@"%ld",i];

            [_notelistArrs addObject:mode];
        
        }
        
    }
    return _notelistArrs;
}

@end
